package com.zimi.controller;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import com.zimi.file.exception.InvalidExtensionException;
import com.zimi.file.service.FileInfoService;
import com.zimi.model.CompanyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "CompanyInfoApi", tags = {"公司信息"})
@RequestMapping("company")
public class CompanyInfoApi {

    private static final String REG_COMPANY = "<table class=\"tableYyzz\">(.*?)</table>";
    private static final String REG_COMPANY_NAME = "企业名称.?<i>(.*?)</i>";
    private static final String REG_COMPANY_CODE = "统一社会信用代码.?<i>(.*?)</i>";
    private static final String REG_COMPANY_TYPE = "类型.?<i>(.*?)</i>";
    private static final String REG_COMPANY_LEGAL = "法定代表人.?<i>(.*?)</i>";
    private static final String REG_COMPANY_CAPITAL = "注册资本.?<i>(.*?)</i>";
    private static final String REG_COMPANY_ST_DATE = "成立日期.?<i>(.*?)</i>";
    private static final String REG_COMPANY_BF_DATE = "经营期限自.?<i>(.*?)</i>";
    private static final String REG_COMPANY_BT_DATE = "经营期限至.?<i>(.*?)</i>";
    private static final String REG_COMPANY_AUTHORITY = "登记机关.?<i>(.*?)</i>";
    private static final String REG_COMPANY_APPROVAL = "核准日期.?<i>(.*?)</i>";
    private static final String REG_COMPANY_STATUS = "登记状态.?<i>(.*?)</i>";
    private static final String REG_COMPANY_ADDRESS = "住所.?<i>(.*?)</i>";
    private static final String REG_COMPANY_SCOPE = "经营范围.?<i>(.*?)</i>";
    @Autowired
    private FileInfoService fileInfoService;

    @PostMapping(value = "info")
    @ApiOperation("根据营业执照获取公司信息")
    public Object info(@RequestParam("file") MultipartFile file) throws Exception {
        Object fl;
        if (file.isEmpty()) {
            return "文件不能为空";
        }
        try {
            fl = fileInfoService.upload(file);
        } catch (InvalidExtensionException e) {
            return e.toString();
        }

        String content = HttpUtil.get(new QRApi().read(file));
        String result = ReUtil.get(REG_COMPANY, content, 1);
        CompanyInfo company = new CompanyInfo();
        company.setName(ReUtil.get(REG_COMPANY_NAME, result, 1))
                .setCode(ReUtil.get(REG_COMPANY_CODE, result, 1))
                .setType(ReUtil.get(REG_COMPANY_TYPE, result, 1))
                .setLegal(ReUtil.get(REG_COMPANY_LEGAL, result, 1))
                .setCapital(ReUtil.get(REG_COMPANY_CAPITAL, result, 1))
                .setSTDate(ReUtil.get(REG_COMPANY_ST_DATE, result, 1))
                .setBFDate(ReUtil.get(REG_COMPANY_BF_DATE, result, 1))
                .setBTDate(ReUtil.get(REG_COMPANY_BT_DATE, result, 1))
                .setAuthority(ReUtil.get(REG_COMPANY_AUTHORITY, result, 1))
                .setApproval(ReUtil.get(REG_COMPANY_APPROVAL, result, 1))
                .setStatus(ReUtil.get(REG_COMPANY_STATUS, result, 1))
                .setAddress(ReUtil.get(REG_COMPANY_ADDRESS, result, 1))
                .setScope(ReUtil.get(REG_COMPANY_SCOPE, result, 1));
        Map<String, Object> map = new HashMap<>();
        map.put("code", null != company.getCode() ? 200 : 500);
        map.put("info", fl);
        map.put("obj", company);
        return map;
    }
}
